import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CustomTextField extends StatelessWidget {
  final TextEditingController textEditingController;
  final String? hintText;
  final Widget? left;
  final Widget? right;
  final Color? backgroundColor;
  final TextInputType? keyboardType;
  final List<TextInputFormatter>? inputFormatters;
  final TextInputAction? textInputAction;
  final bool? enabled;
  final TextAlign textAlign;
  final FocusNode? focusNode;
  final ValueChanged<String>? onSubmitted;
  final ValueChanged<String>? onChanged;
  final int? maxLength;
  final bool obscureText;

  // 错误状态相关
  final bool hasError;
  final String errorText;

  // 样式相关
  // final double height;
  // final BorderRadius? borderRadius;

  const CustomTextField({
    Key? key,
    required this.textEditingController,
    this.hintText,
    this.left,
    this.right,
    this.backgroundColor,
    this.keyboardType,
    this.inputFormatters,
    this.textInputAction,
    this.enabled,
    this.textAlign = TextAlign.start,
    this.focusNode,
    this.onSubmitted,
    this.onChanged,
    this.maxLength,
    this.obscureText = false,
    this.hasError = false,
    this.errorText = '',
    // this.height = 48.0,
    // this.borderRadius,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 根据状态确定边框颜色
    Color borderColor = AppColor.colorF5F7FA; // 默认边框颜色

    if (focusNode != null && focusNode!.hasFocus) {
      borderColor = AppColor.primary; // 输入中的边框为主题色
    }

    if (hasError) {
      borderColor = const Color(0xFFEB5735); // 错误状态边框色为#EB5735
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 输入框
        Container(
          height: 48.w,
          decoration: BoxDecoration(
            color: backgroundColor ?? Colors.white,
            border: Border.all(
              color: borderColor,
              width: 1.w,
            ),
            borderRadius: BorderRadius.circular(40.w),
          ),
          alignment: Alignment.center,
          child: Row(
            children: [
              if (left != null) ...[
                SizedBox(width: 12.w),
                left!,
                SizedBox(width: 8.w),
              ] else
                SizedBox(width: 12.w),
              Expanded(
                child: TextField(
                  controller: textEditingController,
                  obscureText: obscureText,
                  decoration: InputDecoration(
// fillColor: Colors.red,

                    contentPadding: EdgeInsets.zero.copyWith(bottom: 2),
                    border: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    disabledBorder: InputBorder.none,
                    counterText: '',
                    hintText: hintText,
                    hintStyle: TextStyle(
                      color: AppColor.color0C0A09.withOpacity(0.16),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500,
                      fontFamily: 'Outfit',
                    ),
                  ),
                  maxLength: maxLength,
                  inputFormatters: inputFormatters,
                  textInputAction: textInputAction,
                  focusNode: focusNode,
                  textAlign: textAlign,
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                    color: hasError
                        ? const Color(0xFFEB5735)
                        : AppColor.color0C0A09,
                    height: 1.2,
                    fontFamily: 'Outfit',
                  ),
                  keyboardType: keyboardType,
                  enabled: enabled,
                  onSubmitted: onSubmitted,
                  onChanged: onChanged,
                ),
              ),
              if (right != null) ...[
                SizedBox(width: 8.w),
                right!,
                SizedBox(width: 12.w),
              ] else
                SizedBox(width: 12.w),
            ],
          ),
        ),

        // 错误提示
        if (hasError && errorText.isNotEmpty)
          Padding(
            padding: EdgeInsets.only(top: 4.w, left: 4.w),
            child: Row(
              children: [
                Icon(
                  Icons.error_outline,
                  size: 16.w,
                  color: const Color(0xFFEB5735),
                ),
                SizedBox(width: 4.w),
                Text(
                  errorText,
                  style: TextStyle(
                    fontSize: 12.sp,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xFFEB5735),
                  ),
                ),
              ],
            ),
          ),
      ],
    );
  }
}
